﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Globalization;

namespace BadHabit.WPFTools.ValidationRules {

    public class IntRangeRule : System.Windows.Controls.ValidationRule {

        public double? Min { get; set; }
        public double? Max { get; set; }
        
        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            int parameter = 0;

            try {
                if (((string)value).Length > 0) {
                    parameter = Int32.Parse((String)value);
                }
            } catch (Exception e) {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }
            
            if(Min!=null && parameter < Min)
                return new ValidationResult(false, "Please enter value greater or equal to " + Min);

            if (Max != null && parameter > Max)
                return new ValidationResult(false, "Please enter value less or equal to " + Max);

                        
            return new ValidationResult(true, null);
        }
    }
}
